/*
 * 文件名： GoodsAdapter.java
 * 描    述：我的收藏
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */

package com.miguo.adapter;

import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;

import com.miguo.app.AppContext;
import com.miguo.app.AppHelper;
import com.miguo.bean.GoodsBean;
import com.miguo.db.GoodsDB;
import com.miguo.framework.adapter.BaseAdapterHelper;
import com.miguo.framework.adapter.QuickAdapter;
import com.miguo.framework.utils.Toast;
import com.miguo.framework.view.LazyImageView;
import com.miguo.ui.R;
import com.miguo.utils.DateUtils;
import com.miguo.utils.Options;
import com.miguo.utils.ScreenUtils;
import com.miguo.utils.StringUtils;
import com.miguo.view.listener.SingleClickEvent;
import com.mrper.util.ApkUtil;
import com.mrper.util.DeviceUtil;

public class FavorAdapter extends QuickAdapter<GoodsBean> {

    private Activity mActivity;

    private int StartResult;
    private int EndResult;

    public FavorAdapter(Activity activity) {
        super(activity, R.layout.list_goods_favor);
        mActivity = activity;
    }

    @Override
    protected void convert(final BaseAdapterHelper helper, final GoodsBean item) {
        RelativeLayout imageRl = helper.getView(R.id.goods_image_rl);
        imageRl.getLayoutParams().height = mesureImage();

        helper.setText(R.id.goods_title_tv, item.getTitle());

        helper.setImageResource(R.id.goods_favor_iv, R.drawable.goods_favor_sel);
        String CPrice;
        if (StringUtils.isNull(item.getPhone_price())) {
            CPrice = item.getCoupon_price();
        } else {
            CPrice = item.getPhone_price();
        }
        helper.setText(R.id.goods_curprice_tv, CPrice);

        helper.setText(R.id.goods_volume_tv, Volume(item.getVolume()));

        StartResult = DateUtils.getStartTime(item.getBegin_time());
        EndResult = DateUtils.getStartTime(item.getEnd_time());
        if (StartResult == 2 && EndResult != 3) {
            helper.setText(R.id.goods_remind_time_tv, DateUtils.getStrTime(
                    item.getBegin_time(), "yyyy-MM-dd HH:mm:ss")
                    + " 开抢");
            helper.setVisible(R.id.goods_remind_time_tv, View.VISIBLE);
        } else if (EndResult == 3) {
            helper.setTextColor(R.id.goods_title_tv, ApkUtil.getColorById(context,R.color.text_gray_99));
            helper.setTextColor(R.id.goods_curprice_tv, ApkUtil.getColorById(context,R.color.text_gray_99));
            helper.setText(R.id.goods_remind_time_tv, "活动已结束");
            helper.setVisible(R.id.goods_remind_time_tv, View.VISIBLE);
        } else {
            helper.setVisible(R.id.goods_remind_time_tv, View.GONE);
        }

        OnClickListener imgClick = new OnClickListener() {
            @Override
            public void onClick(View v) {
                v.performClick();
            }
        };
        LazyImageView img = helper.getView(R.id.goods_pic_iv);
        img.setNoImgMode(AppContext.isNoImg);
        img.autoDisplayImage(item.getPic_url(),
                Options.getGoodsRoundedOptions(), imgClick);

        helper.setOnClickListener(R.id.goods_favor_rl, new OnClickListener() {

            @Override
            public void onClick(View view) {
                // 取消收藏
                GoodsDB.getInstance(context).delete(item.getItem_id(), 1);
                remove(helper.getPosition());
            }

        });

        helper.getView().setOnClickListener(new SingleClickEvent() {

            @Override
            public void singleClick(View mView) {
                if (DateUtils.getStartTime(item.getBegin_time()) == 2) {
                    Toast.show(
                            context,
                            "亲~该商品将在"
                                    + DateUtils.getStrTime(
                                    item.getBegin_time(), "HH")
                                    + "点准时开抢哦！");
                } else {
                    AppHelper.startGoodsDetailPage((Activity)context,item);//2016-03-14
//                    if (AppContext.isAilError) {
//                        AppHelper.StartTBWebView(mActivity, item, true);
//                    } else {
//                        if (item.getJump_type() == 2 || TextUtils.isEmpty(item.getClick_url())) {
//                            //阿里百川
//                            BaiChuan.showTaokeItemDetailV2(mActivity, item.getTitle(), item.getItem_id(), item.getItem_type());
//                        } else {
//                            if (item.getJump_type() == 3) {
//                                BaiChuan.showPage(mActivity, item.getTitle(), item.getClick_url());
//                            } else {
//                                AppHelper.StartTBWebView(mActivity, item, true);
//                            }
//                        }
//                    }
                }
            }
        });
    }

    private String Volume(Long volume) {
        String result = "0";
        if (volume < 1000) {
            result = volume + "";
        } else if (volume > 1000) {
            double volumeDouble = (double) volume / 1000;
            result = String.format("%.1f%s", volumeDouble, "K");
        }
        return result;
    }

    private int mesureImage() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        Display display = mActivity.getWindowManager().getDefaultDisplay();
        display.getMetrics(displayMetrics);
        int mPageWidth = DeviceUtil.getScreenWidth(context);
        return (mPageWidth - ScreenUtils.dip2px(mActivity, 21.0f)) / 2;//* 185 / 285;
    }
}
